import axios from "axios";

export const baseURL = import.meta.env.VITE_APP_API;
axios.defaults.baseURL = import.meta.env.VITE_APP_API;
axios.defaults.timeout = 3000000;
axios.defaults.withCredentials = false;
let CancelToken = axios.CancelToken;

const _json = { headers: { "Content-Type": "application/json;charset=utf-8" } };
const _form = {
  headers: { "Content-Type": "multipart/form-data;charset=utf-8" },
};
// 添加请求拦截器
axios.interceptors.request.use(
    (config) => {
      const token = localStorage.getItem("token");
      if (token && !config.url.includes('geo.datav.aliyun.com')) {
        config.headers.Authorization = `Bearer ${token}`;
      }
      return config;
    },
    (error) => Promise.reject(error)
);

// get方法
export function get(url, params) {
  return new Promise((resolve, reject) => {
    const nowUrl = axios.defaults.baseURL + url;
    axios
      .get(nowUrl, {
        params: params,
      })
      .then((res) => {
        if (res && res.data) {
          resolve(res.data.data);
        } else {
          reject("请求成功，响应失败");
          console.log(res);
        }
      })
      .catch((err) => {
        reject(err);
      });
  });
}

/**
 * post方法，对应请求
 * @param {String} url [请求的网址]
 * @param {Object} params [请求时携带的参数]
 */
export function post(url, params) {
  return new Promise((resolve, reject) => {
    const nowUrl = axios.defaults.baseURL + url;
    axios
      .post(nowUrl, JSON.stringify(params), _json)
      .then((res) => {
        if (res && res.data) {
          if (res.data.statusCode == 200) {
            resolve(res.data.data);
          } else {
            reject(res.data.data || res.data.errors);
          }
        } else {
          reject("请求成功，响应失败");
          console.log(res);
        }
      })
      .catch((err) => {
        reject("请求失败");
        console.log(err);
      });
  });
}

/**
 * postFile 方法，对应请求
 * @param {String} url [请求的网址]
 * @param {Object} form [请求时携带的表单]
 */
export function postFile(url, file, isApi, cb, cd) {
  axios.defaults.timeout = 300000;
  var form = new FormData();
  form.append("file", file);
  return postForm(url, form, isApi, cb, cd);
}

/**
 * postForm 方法，对应请求
 * @param {String} url [请求的网址]
 * @param {Object} form [请求时携带的表单]
 */
export function postForm(url, form, isApi, cb, cd) {
  return new Promise((resolve, reject) => {
    const nowUrl = axios.defaults.baseURL + url;
    _form.onUploadProgress = (progressEvent) => {
      if (progressEvent.lengthComputable) {
        cb && cb(progressEvent);
      }
    };
    _form.cancelToken = new CancelToken((c) => {
      cd && cd(c);
    });
    console.log(nowUrl, "nowUrl");
    axios
      .post(nowUrl, form, _form)
      .then((res) => {
        if (res && res.status == 200) {
          if (res.data) {
            console.log(res.data)
            resolve({
              fileUrl: res.data.data.fileUrl,
              fileName: res.data.data.newFileName,
            });
          } else {
            reject("请求成功，处理失败");
          }
        } else {
          reject("请求成功，响应失败");
          console.log(res);
        }
        console.log(res, "返回");
      })
      .catch((err) => {
        reject("请求失败");
        console.log(err);
      })
      .finally(() => {
        axios.defaults.timeout = 30000;
      });
  });
}

/**
 * put方法，对应请求
 * @param {String} url [请求的网址]
 * @param {Object} params [请求时携带的参数]
 */
export function put(url, params) {
  return new Promise((resolve, reject) => {
    const nowUrl = axios.defaults.baseURL + url;
    axios
      .put(nowUrl, JSON.stringify(params), _json)
      .then((res) => {
        console.log(res, "res");
        if (res.data && res.data.succeeded) {
          if (res.data.statusCode == 200) {
            resolve(res.data.data);
          } else {
            reject(res.data.data || res.data.errors);
          }
        } else {
          reject("请求成功，响应失败");
          console.log(res);
        }
      })
      .catch((err) => {
        reject(err);
        console.log(err);
      });
  });
}

/**
 * delete方法，对应请求
 * @param {String} url [请求的网址]
 * @param {Object} params [请求时携带的参数]
 */
export function Delete(url, params) {
  return new Promise((resolve, reject) => {
    const nowUrl = axios.defaults.baseURL + url;
    console.log(params, JSON.stringify(params), "params");
    axios({
      method: "delete",
      url: nowUrl,
      data: params,
    })
      .then((res) => {
        console.log(res, "ressss");
        if (res.data && res.data.succeeded) {
          if (res.data.statusCode == 200) {
            resolve(res.data.data);
          } else {
            reject(res.data.data || res.data.errors);
          }
        } else {
          reject(res.data.errors);
          console.log(res);
        }
      })
      .catch((err) => {
        console.log(err, "err");
        reject(err);
      });
  });
}
